
import cv2
import numpy as np
import msvcrt
import time
from MvCameraControl_class import *

def Color_numpy(data, nWidth, nHeight):
    data_ = np.frombuffer(data, count=int(
        nWidth*nHeight*3), dtype=np.uint8, offset=0)
    data_ = data_.reshape([nHeight, nWidth, 3])
    data_ = cv2.cvtColor(data_, cv2.COLOR_BGR2RGB)
    return data_
    data_r = data_[0:nWidth*nHeight*3:3]
    data_g = data_[1:nWidth*nHeight*3:3]
    data_b = data_[2:nWidth*nHeight*3:3]

    data_r_arr = data_r.reshape(nHeight, nWidth)
    data_g_arr = data_g.reshape(nHeight, nWidth)
    data_b_arr = data_b.reshape(nHeight, nWidth)
    numArray = np.zeros([nHeight, nWidth, 3], "uint8")

    numArray[:, :, 2] = data_r_arr
    numArray[:, :, 1] = data_g_arr
    numArray[:, :, 0] = data_b_arr
    return numArray

def Color_numpy1(data, nWidth, nHeight):
    data_ = np.frombuffer(data, count=int(
        nWidth*nHeight*3), dtype=np.uint8, offset=0)
    data_r = data_[0:nWidth*nHeight*3:3]
    data_g = data_[1:nWidth*nHeight*3:3]
    data_b = data_[2:nWidth*nHeight*3:3]

    data_r_arr = data_r.reshape(nHeight, nWidth)
    data_g_arr = data_g.reshape(nHeight, nWidth)
    data_b_arr = data_b.reshape(nHeight, nWidth)
    numArray = np.zeros([nHeight, nWidth, 3], "uint8")

    numArray[:, :, 0] = data_r_arr
    numArray[:, :, 1] = data_g_arr
    numArray[:, :, 2] = data_b_arr
    return numArray

def cam_cvtFrame(cam, pBufAddr, st_frame_info):
    img_buff = None
    buf_cache = None
    numArray = None

    if None == buf_cache:
        buf_cache = (c_ubyte * st_frame_info.nFrameLen)()
    # 获取到图像的时间开始节点获取到图像的时间开始节点
    cdll.msvcrt.memcpy(
        byref(buf_cache), pBufAddr, st_frame_info.nFrameLen)
    # print("get one frame: Width[%d], Height[%d], nFrameNum[%d]" % (
    #     self.st_frame_info.nWidth, self.st_frame_info.nHeight, self.st_frame_info.nFrameNum))
    n_save_image_size = st_frame_info.nWidth * \
        st_frame_info.nHeight * 3 + 2048
    if img_buff is None:
        img_buff = (c_ubyte * n_save_image_size)()


    # RGB直接显示
    if PixelType_Gvsp_RGB8_Packed == st_frame_info.enPixelType:
        numArray = Color_numpy(
            buf_cache, st_frame_info.nWidth, st_frame_info.nHeight)

    # 如果是彩色且非RGB则转为RGB后显示
    else:
        # 转换像素结构体赋值
        stConvertParam = MV_CC_PIXEL_CONVERT_PARAM()
        memset(byref(stConvertParam), 0, sizeof(stConvertParam))
        stConvertParam.nWidth = st_frame_info.nWidth
        stConvertParam.nHeight = st_frame_info.nHeight
        stConvertParam.pSrcData = cast(buf_cache, POINTER(c_ubyte))
        stConvertParam.nSrcDataLen = st_frame_info.nFrameLen
        stConvertParam.enSrcPixelType = st_frame_info.enPixelType
        nConvertSize = st_frame_info.nWidth * st_frame_info.nHeight * 3
        stConvertParam.enDstPixelType = PixelType_Gvsp_RGB8_Packed
        stConvertParam.pDstBuffer = (c_ubyte * nConvertSize)()
        stConvertParam.nDstBufferSize = nConvertSize
        time_start = time.time()
        ret = cam.MV_CC_ConvertPixelType(stConvertParam)
        time_end = time.time()
        # print('MV_CC_ConvertPixelType:', time_end - time_start)
        if ret != 0:
            print('convert pixel fail! ret = ', (ret))
        else:
            cdll.msvcrt.memcpy(
                byref(img_buff), stConvertParam.pDstBuffer, nConvertSize)
            numArray = Color_numpy(
                img_buff, st_frame_info.nWidth, st_frame_info.nHeight)


    if img_buff is not None:
        del img_buff
    if buf_cache is not None:
        del buf_cache
    
    return numArray

